<template>
  <div class="home">
    <!-- 上面的内容 -->
    <div class="container df jcsb">
      <card-vue
        :item="item"
        v-for="item in cardData"
        :key="item.title"
      ></card-vue>
    </div>
    <div class="myChart" ref="chartRef"></div>
  </div>
</template>

<script>
//getTotalDataReq
import {getTotalDataReq} from '@/api/order'
import CardVue from './Card.vue'
import * as echarts from 'echarts'
export default {
  data() {
    return {
      cardData: [],
    }
  },
  components: {
    CardVue,
  },
  methods: {
    //发送初始请求
    async getData() {
      let res = await getTotalDataReq()
      // console.log('res :>> ', res)
      //解构数据
      let {
        xData,
        orderData,
        amountData,
        totalOrder,
        totalAmount,
        todayOrder,
        totayAmount,
      } = res.data
      //处理数据
      let temp = [
        {
          title: '总订单',
          count: totalOrder,
          imgUrl: require('@/assets/img/logo.png'),
        },
        {
          title: '总销售额',
          count: totalAmount,
          imgUrl: require('@/assets/img/logo.png'),
        },
        {
          title: '今日订单数',
          count: todayOrder,
          imgUrl: require('@/assets/img/logo.png'),
        },
        {
          title: '今日销售额',
          count: totayAmount,
          imgUrl: require('@/assets/img/logo.png'),
        },
      ]
      //数据赋值
      this.cardData = temp
      //处理数据
      let series = [
        {
          type: 'line',
          name: '金额数据',
          data: amountData,
        },
        {
          type: 'line',
          name: '订单数据',
          data: orderData,
        },
      ]

      //画图
      this.renderLine({
        series,
        xData,
      })
    },
    //画折线图
    renderLine(obj) {
      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(this.$refs.chartRef)
      //配置项
      let options = {
        title: {
          text: 'Stacked Line',
        },
        tooltip: {
          trigger: 'axis',
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: obj.xData,
        },
        yAxis: {
          type: 'value',
        },
        series: obj.series,
      }
      // 绘制图表
      myChart.setOption(options)
    },
  },
  created() {},
  mounted() {
    this.getData()
  },
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .myChart {
    padding: 20px;
    margin-top: 30px;
    width: 100%;
    height: 360px;
    background: #fff;
  }
}
</style>
